const express = require("express")
const router = express.Router()
const bokeServ = require("../../services/bokeservice")
const bokeServs = require("../../services/bokecontentservice")

const {
  asyncHandler
} = require("../getSendResult");

router.get("/", asyncHandler(async (req, res, next) => {
  const page = req.query.page || 1;
  const limit = req.query.limit || 5;
  return await bokeServ.getfindall(page, limit);
}))
router.get("/getfindbk", asyncHandler(async (req, res, next) => {
  const id = req.query.id
  return await bokeServs.getIdfind(id);
}))

router.get("/getfindboke", asyncHandler(async (req, res, next) => {
  const id = req.query.id
  return await bokeServ.getIdfind(id);
}))

router.post("/getBookbyname", asyncHandler(async (req, res, next) => {
  const page = req.body.page || 1;
  const limit = req.body.limit || 5;
  const sousuo = req.body.sousuo
  return await bokeServ.getBookbyname(page, limit, sousuo);
}))

router.post("/getBookbyfenlei", asyncHandler(async (req, res, next) => {
  const page = req.body.page || 1;
  const limit = req.body.limit || 5;
  const fenlei = req.body.fenlei
  return await bokeServ.getBookbyfenlei(page, limit, fenlei);
}))


router.post("/addBoke", asyncHandler(async (req, res, next) => {
  if (req.body.text) {
    const Boketit = req.body.tit
    const Bokecontent = req.body.text
    const article = req.body.text
    const fenlei = req.body.fenlei
    const ReleaseTime = req.body.date
    const Reading = req.body.yuedu
    const bokeId = req.body.conut
    await bokeServ.addBoke({
      Boketit,
      Bokecontent,
      fenlei,
      ReleaseTime,
      Reading
    })
    await bokeServs.addBoke({
      article,
      bokeId
    })
    return

  } else {
    throw new Error("Text is not null !!!")
  }
}))

router.post("/addBokecontent", asyncHandler(async (req, res, next) => {
  if (req.body.text) {
    const article = req.body.text
    return await bokeServs.addBoke({
      article,
    })
  } else {
    throw new Error("Text is not null !!!")
  }
}))

router.get("/getReading", asyncHandler(async (req, res, next) => {
  const page = req.query.page || 1;
  const limit = req.query.limit || 5;
  return await bokeServ.getreading(page, limit)
}))

router.get("/updateboke", asyncHandler(async (req, res, next) => {
  const id = req.query.id
  const Reading = req.query.yuedu
  return await bokeServ.updateBoke(id, Reading)
}))

module.exports = router